﻿using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SharpShell.Attributes;
using SharpShell.SharpContextMenu;

namespace GurLoaderHelper
{
    [ComVisible(true)]
    [COMServerAssociation(AssociationType.Class, "jpegfile")]
    [COMServerAssociation(AssociationType.Class, "pngfile")]
    [COMServerAssociation(AssociationType.Class, "giffile")]
    [COMServerAssociation(AssociationType.Class, "TIFImage.Document")]
    [COMServerAssociation(AssociationType.Class, "Paint.Picture")]
    public class GurLoaderExtension : SharpContextMenu
    {
        private const string DEFAULT_MAILSLOTNAME = "GurLoader";

        private IMessagePublisher MessagePublisher { get; set; }
        private IMenuProvider MenuProvider { get; set; }
        public GurLoaderExtension() : this(DEFAULT_MAILSLOTNAME)
        {
            
        }

        public GurLoaderExtension(string mailslotName) : this( new MailslotMessagePublisher(mailslotName), new DefaultMenuProvider() ) 
        {
        }

        public GurLoaderExtension(IMessagePublisher messagePublisher, IMenuProvider menuProvider)
        {
            if (messagePublisher == null)
                throw new ArgumentNullException("messagePublisher");

            if (menuProvider == null)
                throw new ArgumentNullException("menuProvider");

            MessagePublisher = messagePublisher;
            MenuProvider = menuProvider;

            MenuProvider.CommandGenerated += (sender, args) =>
            {
                string command = string.Format("{0} {1}", args.Command, args.Parameters);
                MessagePublisher.SendMessage(command);
            };
        }

        protected override bool CanShowMenu()
        {
            return MenuProvider.CanShowMenu(this.SelectedItemPaths);
        }

        protected override ContextMenuStrip CreateMenu()
        {
            return MenuProvider.CreateMenu(this.SelectedItemPaths);
        }
    }
}
